package com.yangcangclub.trade.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.utils.StringUtil;
import com.yangcangclub.merchandise.bean.Product;
import com.yangcangclub.trade.base.TradeBaseService;
import com.yangcangclub.trade.bean.*;
import com.yangcangclub.trade.service.TradeService;
import com.yangcangclub.trade.service.UserAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/2/24.
 */
@Service
public class UserAddressServiceImpl extends TradeBaseService implements UserAddressService {

    private static Logger logger = LoggerFactory.getLogger(UserAddressServiceImpl.class);

    /**保存用户地址
     * @param userAddress
     * @return
     */
    @Override
    @Transactional(rollbackFor= ApolloException.class)
    public boolean saveUserAddress(UserAddress userAddress) {
        if(userAddressDomain.insertUserAddress(userAddress)>0){
            return true;
        }
        return false;
    }

    /**删除用户信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteUserAddressById(int id) {
        return userAddressDomain.deleteUserAddressById(id);
    }

    /**设置某个地址为默认地址
     * @param userId
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor= ApolloException.class)
    public boolean updateUserAddressDefault(int userId, int id) {
        return userAddressDomain.updateUserAddressDefaultById(userId, id);
    }

    /**更新地址信息
     * @param userAddress
     * @return
     */
    @Override
    @Transactional(rollbackFor= ApolloException.class)
    public boolean updateUserAddress(UserAddress userAddress) {
       return userAddressDomain.updateUserAddressById(userAddress);
    }

    /**根据用户id获取地址列表
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> getUserAddressList(int userId) {
        return userAddressDomain.selectValidUserAddressByUserId(userId);
    }

    /**根据地址id获取地址信息
     * @param id
     * @return
     */
    @Override
    public UserAddress getUserAddressDetails(int id) {
        return userAddressDomain.selectUserAddressById(id);
    }

    /**获取默认地址
     * @param userId
     * @return
     */
    @Override
    public UserAddress getDefaultUserAddress(int userId) {
        return userAddressDomain.getDefaultUserAddress(userId);
    }

    @Override
    public int getUserAddressListCount(int userId) {
        return userAddressDomain.selectValidUserAddressByUserIdCount(userId);
    }
}
